package org.dingyom.basics.util;

import org.csource.fastdfs.*;

import java.io.File;

public class FastdfsUtil {
    //加载配置文件 配置文件中的内容就是 tracker 服务的地址
    public static String CONF_FILENAME  = FastdfsUtil.class.getClassLoader()
                                            .getResource("fdfs_client.conf")
                                            .getFile();

    /**
     * 上传文件
     * @param file ：byte数组格式的文件内容
     * @param extName ：文件扩展名
     * @return
     */
    public static  String upload(byte[] file,String extName) {

        try {
            //初始化配置
            ClientGlobal.init(CONF_FILENAME);
            /*创建Tracker客户端*/
            //创建一个 TrackerClient 对象
            TrackerClient tracker = new TrackerClient();
            //使用 TrackerClient 对象创建连接，获得一个 TrackerServer 对象
            TrackerServer trackerServer = tracker.getConnection();
            //创建一个 StorageServer 的引用
            StorageServer storageServer = null;
            //创建Storage 客户端.需要两个参数 TrackerServer 对象、StorageServer 的引用
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            //设置而外的参数
            /**
             NameValuePair nvp [] = new NameValuePair[]{
             new NameValuePair("width", "100"),
             new NameValuePair("height", "100")
             }; **/

            //执行上传 返回文件的id
            String fileIds[] = storageClient.upload_file(file,extName,null);
            //拼接成路径
            String path =  "/"+fileIds[0]+"/"+fileIds[1];
            System.out.println("文件上传成功，path="+path);
            return path;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }
    /**
     * 上传文件
     * @param extName
     * @return
     */
    public static  String upload(String path,String extName) {

        try {
            ClientGlobal.init(CONF_FILENAME);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            String fileIds[] = storageClient.upload_file(path, extName,null);

            System.out.println(fileIds.length);
            return  "/"+fileIds[0]+"/"+fileIds[1];

        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    /**
     * 下载文件
     * @param groupName
     * @param fileName
     * @return
     */
    public static byte[] download(String groupName,String fileName) {
        try {

            ClientGlobal.init(CONF_FILENAME);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer, storageServer);


            return storageClient.download_file(groupName, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }


    /**
     * 删除文件
     * @param path 文件路径
     */
    public static boolean delete(String path){
        try {
            ClientGlobal.init(CONF_FILENAME);

            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            //获取groupName 和 fileName
            //  /group1/M00/00/00/rBEAA2MVb-uAf4AxAAB7d5UENyQ376.jpg
            path = path.substring(1);//把索引0处的/ 给删去
            int firstPosition = path.indexOf("/");//返回第一个/的索引
            String groupName = path.substring(0, firstPosition);//截取处group1
            String fileName = path.substring(firstPosition + 1);//从 第一个/后一个索引处开始截取 M00/00/00/rBEAA2MVb-uAf4AxAAB7d5UENyQ376.jpg
            int i = storageClient.delete_file(groupName,fileName);
            System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
            return i == 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException("删除异常,"+e.getMessage());
        }
    }

    public static void main(String[] args) {
        //上传文件
//        String jpg = upload("D:\\自写\\image\\F6.jpg", "jpg");
//        System.out.println(jpg);

        //删除
        boolean delete = delete("/group1/M00/00/00/rBEAA2MVpLuAdn4yAAWJ4_buT14913.jpg");
        System.out.println(delete);
    }
}
